package com.gjy.arthas.sps.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.gjy.arthas.sps.sentinel.HelloSentinel;
import com.gjy.arthas.sps.service.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-10-28 09:40:04
 */
@RestController
@RequestMapping("/hello")
public class HelloController {
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);

    @Resource
    private HelloService helloService;

    @GetMapping("/h1")
    @SentinelResource(value = "com.gjy.arthas.sps.controller.HelloController.h1",
            blockHandlerClass = HelloSentinel.class,
            blockHandler = "handleBlock",
            fallbackClass = HelloSentinel.class,
            fallback = "fallback")
    public String h1(@RequestParam String type) {
        if (type.equals("1")) {
            return LocalDateTime.now().toString();
        } else {
            throw new RuntimeException("aaa");
        }
    }

    @PostMapping
    public Object hello(@RequestBody Map<String, Object> request) throws UnknownHostException {
        log.info("hello: {}", request);
        // TimeUnit.SECONDS.sleep(2);
        String hello = helloService.hello();
        log.info("H: {}", hello);
        InetAddress inetAddr = InetAddress.getLocalHost();
        String hostName = inetAddr.getHostName();
        log.info("host-name: {}", hostName);
        return request;
    }

}
